const mysql  = require('mysql')
const dbConfig = require('../../dbs/config')

const db = {
    query : function(sql,params,callback){
        let connection = mysql.createConnection(dbConfig.DB.relationalConfs[0]);        
        connection.connect(function(err){
            if(err){
                console.log('数据库链接失败');
                throw err;
            }
        connection.query( sql, params, function(err,results,fields ){
           if(err){
                console.log('数据操作失败');
                throw err;
            }
            callback && callback(results, fields);
              connection.end(function(err){
                  if(err){
                      console.log('关闭数据库连接失败！');
                      throw err;
                  }
              });
           });
       });
    }
}

module.exports = db

// let connection = mysql.createConnection(dbConfig.DB.relationalConfs[0])


// connection.connect();
 
// let  sql = 'SELECT * FROM book';
// connection.query(sql,function (err, result) {
//         if(err){
//           console.log('[SELECT ERROR] - ',err.message);
//           return;
//         }
 
//        console.log('--------------------------SELECT----------------------------');
//        console.log(result);
//        console.log('------------------------------------------------------------\n\n');  
// });
// connection.end();